$(function () {
    var name = $("#name"),
        sex = $("#sex"),
        image = $("#image"),
        rank = $("#rank"),
        parentId = $("#parentId"),
        picturePre = $("#picturePre"),
        //allFields=[name,parentId,rank,picturePre],
        allFields = $([]).add(name).add(parentId).add(rank).add(picturePre),
        tips = $(".validateTips");

    function updateTips(t) {
        tips
            .text(t)
            .addClass("ui-state-highlight");
        setTimeout(function () {
            tips.removeClass("ui-state-highlight", 1500);
        }, 500);
    }

    function checkLength(o, n, min, max) {
        if (o.val().length > max || o.val().length < min) {
            o.addClass("ui-state-error");
            updateTips("" + n + " 的长度必须在 " +
                min + " 和 " + max + " 之间。");
            return false;
        } else {
            return true;
        }
    }

    function checkEmpity(o, n) {
        if (o.val() != null && o.val().length > 0) {
            return true
        } else {
            o.addClass("ui-state-error");
            updateTips("" + n + " 不能为空 ");
            return false;
        }
    }

    function checkRegexp(o, regexp, n) {
        if (!(regexp.test(o.val()))) {
            o.addClass("ui-state-error");
            updateTips(n);
            return false;
        } else {
            return true;
        }
    }

    $("#dialog-form").dialog({
        autoOpen: false,
        height: 500,
        width: 550,
        modal: true,
        buttons: {
            "添加": function () {
                var bValid = true;
                allFields.removeClass("ui-state-error");
                // 开始校验
                bValid = bValid && checkEmpity(name, "器官名称");
                bValid = bValid && checkLength(parentId, "上级id", 1, 10);
                bValid = bValid && checkEmpity(rank, "Rank");
                bValid = bValid && checkRegexp(rank, /^[1-9]\d*(;[1-9]\d*)*$/, "rank格式不正确。");
                if (picturePre.val() != null && picturePre.val().length > 0) {
                    bValid = bValid && checkEmpity(image, "图片");
                    bValid = bValid && checkRegexp(image, /.*\.[png,PNG]/, "图片格式必须为PNG。");
                } else if (image.val() != null && image.val().length > 0) {
                    bValid = bValid && checkEmpity(picturePre, "图片前缀");
                }


                // 如果校验成功
                if (bValid) {
                    upload("/body/editOrgan.do", {
                        "name": name.val(),
                        "sex": sex.val(),
                        "rank": rank.val(),
                        "parentId": parentId.val(),
                        "picturePre": picturePre.val()
                    }, this);
                }
            },
            Cancel: function () {
                $(this).dialog("close");
            }
        },
        close: function () {
            allFields.val("").removeClass("ui-state-error");
        }
    });

    $("#add-class").button().click(function () {
        $("#dialog-form").dialog("open");
    });
})

function upload(url, organForm, obj) {
    $.ajaxFileUpload({
        url: url,
        secureuri: false,
        fileElementId: 'image',
        dataType: 'json',
        data: organForm,
        success: function (res) {
            if (res.code === '0') {
                alert("成功");
                window.location.href = "/body/organ_list.htm?tabid=103_1";
            } else {
                alert(res.desc);
            }
        },
        error: function (XMLHttpRequest, textStatus, errorThrown) {
            if ($("#errorRequestStatus")) {
                $("#errorRequestStatus").val(XMLHttpRequest.status);
            }
            if ($("#errorRequestReadyState")) {
                $("#errorRequestReadyState").val(
                    XMLHttpRequest.readyState);
            }
            if ($("#errorTextStatus")) {
                $("#errorTextStatus").val(textStatus);
            }
            // 如果在一个方法调用未结束直接切换页面会包状态为0的错误，屏蔽掉
            if (XMLHttpRequest.status != 0) {
                alert('系统异常，请与管理员联系');
            }
        },
        complete: function () {
        }
    });
}

function deleteOrgan(id) {
    API.deleteOrgan({"id": id}, function (res) {
        if (res.code === "0") {
            alert("删除成功");
            window.location.reload();
        } else {
            alert(res.desc);
        }
    })
}

function editOrgan(idV, nameV, sexV, parentIdV, rankV) {
    var id = $("#id"),
        name = $("#name"),
        sex = $("#sex"),
        image = $("#image"),
        rank = $("#rank"),
        parentId = $("#parentId"),
        picturePre = $("#picturePre"),
        //allFields=[name,parentId,rank,picturePre],
        allFields = $([]).add(name).add(parentId).add(rank).add(picturePre),
        tips = $(".validateTips");

    function updateTips(t) {
        tips
            .text(t)
            .addClass("ui-state-highlight");
        setTimeout(function () {
            tips.removeClass("ui-state-highlight", 1500);
        }, 500);
    }

    function checkLength(o, n, min, max) {
        if (o.val().length > max || o.val().length < min) {
            o.addClass("ui-state-error");
            updateTips("" + n + " 的长度必须在 " +
                min + " 和 " + max + " 之间。");
            return false;
        } else {
            return true;
        }
    }

    function checkEmpity(o, n) {
        if (o.val() != null && o.val().length > 0) {
            return true
        } else {
            o.addClass("ui-state-error");
            updateTips("" + n + " 不能为空 ");
            return false;
        }
    }

    function checkRegexp(o, regexp, n) {
        if (!(regexp.test(o.val()))) {
            o.addClass("ui-state-error");
            updateTips(n);
            return false;
        } else {
            return true;
        }
    }

    $("#dialog-form").dialog({
        autoOpen: false,
        height: 500,
        width: 550,
        modal: true,
        buttons: {
            "提交": function () {
                var bValid = true;
                allFields.removeClass("ui-state-error");
                // 开始校验
                bValid = bValid && checkEmpity(name, "器官名称");
                bValid = bValid && checkLength(parentId, "上级id", 1, 10);
                bValid = bValid && checkEmpity(rank, "Rank");
                bValid = bValid && checkRegexp(rank, /^[1-9]\d*(;[1-9]\d*)*$/, "rank格式不正确。");
                if (picturePre.val() != null && picturePre.val().length > 0) {
                    bValid = bValid && checkEmpity(image, "图片");
                    bValid = bValid && checkRegexp(image, /.*\.[png,PNG]/, "图片格式必须为PNG。");
                } else if (image.val() != null && image.val().length > 0) {
                    bValid = bValid && checkEmpity(picturePre, "图片前缀");
                }

                // 如果校验成功
                if (bValid) {
                    upload("/body/editOrgan.do", {
                        "id": id.val(),
                        "name": name.val(),
                        "sex": sex.val(),
                        "rank": rank.val(),
                        "parentId": parentId.val(),
                        "picturePre": picturePre.val()
                    }, this);
                }
            },
            Cancel: function () {
                $(this).dialog("close");
            }
        },
        close: function () {
            allFields.val("").removeClass("ui-state-error");
        }
    });
    id.val(idV);
    name.val(nameV);
    sex.val(sexV);
    parentId.val(parentIdV);
    rank.val(rankV);
    $("#dialog-form").dialog("open");
}

function exportDate(obj, code) {
    if (UTIL.checkButton(obj)) {

        API.managementMigrateExcute({"code": code}, function (res) {
            if (res.code == '0') {
                alert("执行成功！");
            } else {
                alert(res.desc);
            }
        });

        UTIL.releaseButton(obj);
    }
}